package com.jackchong.net;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonWriter;
import com.jackchong.utils.JLog;

import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.annotation.Annotation;
import java.nio.charset.Charset;

import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import okio.Buffer;
import retrofit2.Converter.Factory;
import retrofit2.Retrofit;


public class JGsonConverterFactory extends Factory {
    private static Gson gson;

    public static JGsonConverterFactory create() {
        GsonBuilder gsonBuilder = new GsonBuilder().serializeNulls();
        gson = gsonBuilder.create();
        return create(gson);
    }

    public static JGsonConverterFactory create(Gson gson) {
        return new JGsonConverterFactory(gson);
    }


    private JGsonConverterFactory(Gson gson) {
        if (gson == null) throw new NullPointerException("gson == null");
        JGsonConverterFactory.gson = gson;
    }

    @Override
    public retrofit2.Converter<ResponseBody, ?> responseBodyConverter(java.lang.reflect.Type type, Annotation[] annotations,
                                                                      Retrofit retrofit) {
        return new LenientGsonResponseBodyConverter<>(gson, type);
    }

    @Override
    public retrofit2.Converter<?, RequestBody> requestBodyConverter(java.lang.reflect.Type type,
                                                                    Annotation[] parameterAnnotations, Annotation[] methodAnnotations, Retrofit retrofit) {
        com.google.gson.TypeAdapter<?> adapter = gson.getAdapter(TypeToken.get(type));
        return new LenientGsonRequestBodyConverter<>(gson, adapter);
    }

    public class LenientGsonResponseBodyConverter<T> implements retrofit2.Converter<ResponseBody, T> {
        private final Gson gson;
        private final java.lang.reflect.Type type;

        LenientGsonResponseBodyConverter(Gson gson, java.lang.reflect.Type type) {
            this.gson = gson;
            this.type = type;
        }

        @Override
        public T convert(ResponseBody value) throws IOException {
            String response = value.string();
            JLog.json("jhttp", response);
            return gson.fromJson(response, type);
        }
    }

    public class LenientGsonRequestBodyConverter<T> implements retrofit2.Converter<T, RequestBody> {
        private final MediaType MEDIA_TYPE = MediaType.parse("application/json; charset=UTF-8");
        private final Charset UTF_8 = Charset.forName("UTF-8");
        private final Charset GBK = Charset.forName("GBK");

        private final Gson gson;
        private final com.google.gson.TypeAdapter<T> adapter;

        LenientGsonRequestBodyConverter(Gson gson, com.google.gson.TypeAdapter<T> adapter) {
            this.gson = gson;
            this.adapter = adapter;
        }

        @Override
        public RequestBody convert(T value) throws IOException {
            Buffer buffer = new Buffer();
            java.io.Writer writer = new OutputStreamWriter(buffer.outputStream(), UTF_8);
            JsonWriter jsonWriter = gson.newJsonWriter(writer);

            jsonWriter.setLenient(true);
            adapter.write(jsonWriter, value);
            jsonWriter.close();
            return RequestBody.create(MEDIA_TYPE, buffer.readByteString());
        }
    }
}
